package cc.iteachyou.template.taglib.directives;

import cc.iteachyou.template.dom.VirtualDOMAttr;
import cc.iteachyou.template.dom.VirtualDOMNode;
import cc.iteachyou.template.taglib.IDirectiveHandler;
import cc.iteachyou.template.taglib.result.DirectiveResult;
import cc.iteachyou.template.utils.VariableHelper;

import java.util.Map;
import java.util.Optional;

public class ScriptDirectiveHandler implements IDirectiveHandler {
    @Override
    public Optional<DirectiveResult> handler(VirtualDOMNode.RendererVirtualDOMAttrContext ctx, Map<String, Object> data) {
        VirtualDOMAttr attr = ctx.getAttr();

        VariableHelper variableHelper = VariableHelper.getInstance();
        variableHelper.get().put(attr.getName(), attr.getValue());

        DirectiveResult result = DirectiveResult.builder()
                .name(attr.getName())
                .value(attr.getValue())
                .build();
        return Optional.of(result);
    }
}
